import fs from 'node:fs';
import { prompt } from '../utils';
import { SAVE_PATH } from './config';

export type UserInfoType = {
  password: string;
  userName: string;
};
export default async function inputUserInfo(): Promise<UserInfoType> {
  const promptConfig = [
    {
      type: 'input',
      name: 'userName',
      message: 'jenkins用户名',
     },
     {
      type: 'password',
      name: 'password',
      message: 'jenkins密码',
    },
   ];
   const result = await prompt(promptConfig);
   const userName = result?.userName;
   const password = result?.password;
   
   if (!userName||!password) {
     throw  new Error("用户名或密码错误");
   }
   const cache:UserInfoType = { userName, password };
   fs.writeFileSync(SAVE_PATH, JSON.stringify(cache));  
   return cache;
}